package com.wgw.first.utils;

import android.content.Context;
import android.widget.Toast;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.wgw.first.cart.utils.CartStorage.JSON_CART;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public  class UpCartData {


    //上传购物出数据到服务器
    public static void  sendRequestWithOkHttp(final Context context) {
        if(Constants.ALEADYLOGIN==""){
            //如果用户未登录，则不同步数据库
            Toast.makeText(context, "未登录，将在本地数据库操作", Toast.LENGTH_SHORT).show();
        }else{
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String json_cart = CacheUtils.getString(context, JSON_CART)+"@"+Constants.ALEADYLOGIN;
                        System.out.println("上传购物车数据MainActivity:json_cart" + json_cart);
                        RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=utf-8"),
                                "jsoncart="+json_cart);
                        //                    RequestBody requestBody = new FormBody.Builder()
                        //                            .add("jsoncart",json_cart)
                        //                            .build();
                        OkHttpClient client = new OkHttpClient();
                        Request request = new Request.Builder()
                                .url(Constants.BASE + Constants.UPCARTJSON)
                                .post(requestBody)
                                .build();
                        Response response = client.newCall(request).execute();
                        String responsedata = response.body().string();
                        System.out.println("responsedata" + responsedata);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }


}
